﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Net.Mail;

public partial class Contact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
	    
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
	    // Reset error status
	    lblErr.Text = "";
	    lblErr.ForeColor = System.Drawing.Color.Red;
	    reCaptcha.BackColor = System.Drawing.Color.Empty;
	    txtEmail.BackColor = System.Drawing.Color.Empty;
	    txtName.BackColor = System.Drawing.Color.Empty;
	    txtMessage.BackColor = System.Drawing.Color.Empty;

	    // Validate E-mail Address
	    if (!new System.Text.RegularExpressions.Regex(ConfigurationManager.AppSettings["EmailAddressRegex"]).IsMatch(txtEmail.Text)) {
		    lblErr.Text = "Error: Email address failed Regex check. Please enter a valid E-mail address.";
		    txtEmail.BackColor = System.Drawing.Color.Red;
		    return; }
	    if (txtEmail.Text.Substring(txtEmail.Text.IndexOf('.') + 1).Length < 2) {
		    lblErr.Text = "Error: Address domain TLD (" + System.Web.HttpUtility.HtmlEncode(txtEmail.Text.Substring(txtEmail.Text.IndexOf('.') + 1)) + ") is too short to be real.";
		    txtEmail.BackColor = System.Drawing.Color.Red;
		    return; }

	    // Validate other fields
	    if (string.IsNullOrEmpty(txtMessage.Text.Trim()))
	    {
		    lblErr.Text = "Error: Your message is blank; How shall I know what you want?";
		    txtMessage.BackColor = System.Drawing.Color.Red;
		    return;
	    }
	    if (string.IsNullOrEmpty(txtName.Text.Trim())) {
		    lblErr.Text = "Error: Your name is blank; How shall I know what to call you?";
		    txtName.BackColor = System.Drawing.Color.Red;
		    return;
	    }

	    // Validate CAPTCHA
	    if (!reCaptcha.IsValid)
	    {
		    lblErr.Text = "Error: Unable to validate that you are human. Message was: " + reCaptcha.ErrorMessage;
		    reCaptcha.BackColor = System.Drawing.Color.Red;
		    return;
	    }
	    
	    // Prepare message
	    string body =
		    "A Contact has been initiated from Multimon Online's Contact Us page. Info follows: <br /><br />\r\n\r\n" +
		    "From: " + System.Web.HttpUtility.HtmlEncode(txtName.Text) + "<br /><br />\r\n" +
		    "E-mail: " + System.Web.HttpUtility.HtmlEncode(txtEmail.Text) + "<br /><br />\r\n" +
		    "Message Body: \r\n" + System.Web.HttpUtility.HtmlEncode(txtMessage.Text.Replace("\r\n","<br />"));

	    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(
		    ConfigurationManager.AppSettings["AppMailFrom"],
		    ConfigurationManager.AppSettings["AdminMailTo"],
		    ConfigurationManager.AppSettings["ContactSubject"],
		    body);
	    message.Sender = new MailAddress(ConfigurationManager.AppSettings["AppMailFrom"], ConfigurationManager.AppSettings["MailDisplayName"]);
	    message.IsBodyHtml = true;

	    // Send message
	    SmtpClient sc = new SmtpClient(ConfigurationManager.AppSettings["MailServer"]);
	    sc.UseDefaultCredentials = false;
	    sc.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailUser"], ConfigurationManager.AppSettings["SmtpPassword"]);
	    sc.DeliveryMethod = SmtpDeliveryMethod.Network;

	    try { 
		    sc.Send(message);
		    btnSubmit.Enabled = false;
		    lblErr.Text = "Message sent successfully.";
		    lblErr.ForeColor = System.Drawing.Color.Green;
	    }
	    catch (System.Net.Mail.SmtpFailedRecipientException)
	    {
		    lblErr.Text = "Sorry, your mail was rejected by my mail server. I have some people to yell at.";
	    }
	    catch (System.Net.Mail.SmtpException ex)
	    {
		    if (ex.ToString().Contains("Mailbox name not allowed. The server response was: Sorry, over your daily relay limit."))
			    lblErr.Text = "Sorry, I'm over my SMTP Relay Limit for the day.";
		    else lblErr.Text = "Error: " + ex.Message;
	    }
	    catch (Exception ex)
	    {
		    lblErr.Text = "Error: " + ex.Message;
	    }
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
	    txtEmail.Text = "";
	    txtMessage.Text = "";
	    txtName.Text = "";
	    lblErr.Text = "";
	    txtEmail.BackColor = System.Drawing.Color.White;
	    txtMessage.BackColor = System.Drawing.Color.White;
	    txtName.BackColor = System.Drawing.Color.White;
	    lblErr.ForeColor = System.Drawing.Color.Red;

    }

}
